﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;

namespace Grizzy_X
{
    public class Viewer
    {
        private double Displayscale;
        private double OffsetX;      //Offset为屏幕左上角的实际坐标
        private double OffsetY;

        public double GetScale()
        {
            return Displayscale;
        }

        public double GetX()
        {
            return OffsetX;
        }

        public double GetY()
        {
            return OffsetY;
        }

        public void initializeViewer(double Display, double offset_X, double offset_Y)
        {
            this.Displayscale = Display;
            this.OffsetX = offset_X;
            this.OffsetY = offset_Y;
        }


          public bool ZoomIn_Point(Point unchange)
        {
            try
            {
                PointF unchange_real = new PointF();
                unchange_real = Transformer.FromScreen(unchange);

                for (int i = 0; i < Controller.MapLegends.Count; i++)
                {
                    if (Controller.MapLegends[i].ByScale == true)
                    {
                        switch (Controller.MapLegends[i].Legend_Type)
                        {
                            case CLegend.LegendType.CPointLegend:
                                ((CPointLegend)Controller.MapLegends[i]).size /= (float)Displayscale;
                                break;
                            case CLegend.LegendType.CLineLegend:
                                ((CLineLegend)Controller.MapLegends[i]).line_weight /= (float)Displayscale;
                                break;
                            case CLegend.LegendType.CPolygonLegend:
                                ((CPolygonLegend)Controller.MapLegends[i]).border_weight /= (float)Displayscale;
                                break;
                            case CLegend.LegendType.CTextLegend:
                                ((CTextLegend)Controller.MapLegends[i]).font = new Font("SimHei", ((CTextLegend)Controller.MapLegends[i]).font.Size / (float)Displayscale);
                                break;
                        }
                    }
                }



                Displayscale=Displayscale*1.5;//默认放大一倍
                if (Controller.MyMap.Unit == "英寸")
                {
                    OffsetX = unchange_real.X - unchange.X / (Displayscale * 96);
                    OffsetY = unchange.Y / (Displayscale * 96) + unchange_real.Y;
                }
                else
                {
                    OffsetX = unchange_real.X - unchange.X * 2.54 / (Displayscale * 96);
                    OffsetY = unchange.Y * 2.54 / (Displayscale * 96) + unchange_real.Y;
                }
                Transformer.Displayscale = Displayscale;
                Transformer.OffsetX = OffsetX;
                Transformer.OffsetY = OffsetY;

                //如果图例是byscale的，则使图例跟着放大
                for (int i = 0; i < Controller.MapLegends.Count; i++)
                {
                    if (Controller.MapLegends[i].ByScale == true)
                    {
                        switch (Controller.MapLegends[i].Legend_Type)
                        {
                            case CLegend.LegendType.CPointLegend:
                                ((CPointLegend)Controller.MapLegends[i]).size *= (float)Displayscale;
                                CPointLegend legend = (CPointLegend)Controller.MapLegends[i];
                                Bitmap preview = new Bitmap((int)(legend.size) + 2, (int)(legend.size) + 2);//画笔默认为两个像素的宽度

                                Graphics gra = Graphics.FromImage(preview);

                                Pen pen = new Pen(legend.color, 2);
                                SolidBrush brush = new SolidBrush(legend.color);
                                Point[] points = new Point[4];
                                //三角形坐标
                                points[0].X = (int)(0.5 * legend.size); points[0].Y = (int)(legend.size - Math.Sqrt(3) * 0.5 * legend.size);
                                points[1].X = (int)(legend.size); points[1].Y = (int)(legend.size);
                                points[2].X = 0; points[2].Y = (int)(legend.size);
                                points[3] = points[0];
                                float tempx;
                                tempx = (float)(legend.size);
                                if (legend.Point_Style == CPointLegend.PointStyle.circle)
                                    gra.DrawEllipse(pen, 0, 0, tempx, tempx);
                                else if (legend.Point_Style == CPointLegend.PointStyle.filled_circle)
                                    gra.FillEllipse(brush, 0, 0, tempx, tempx);
                                else if (legend.Point_Style == CPointLegend.PointStyle.rectangle)
                                    gra.DrawRectangle(pen, 0, 0, legend.size, legend.size);
                                else if (legend.Point_Style == CPointLegend.PointStyle.filled_rectangle)
                                    gra.FillRectangle(brush, 0, 0, tempx, tempx);
                                else if (legend.Point_Style == CPointLegend.PointStyle.triangle)
                                    gra.DrawLines(pen, points);
                                else if (legend.Point_Style == CPointLegend.PointStyle.filled_triangle)
                                    gra.FillPolygon(brush, points);
                                else
                                    gra.DrawEllipse(pen, 0, 0, tempx, tempx);
                                ((CPointLegend)Controller.MapLegends[i]).cpointlegend = preview;
                                break;
                            case CLegend.LegendType.CLineLegend:
                                ((CLineLegend)Controller.MapLegends[i]).line_weight *= (float)Displayscale;
                                break;
                            case CLegend.LegendType.CPolygonLegend:
                                ((CPolygonLegend)Controller.MapLegends[i]).border_weight *= (float)Displayscale;
                                break;
                            case CLegend.LegendType.CTextLegend:
                                ((CTextLegend)Controller.MapLegends[i]).font = new Font("SimHei", ((CTextLegend)Controller.MapLegends[i]).font.Size * (float)Displayscale);
                                break;
                        }
                    }
                }

                return true;
            }
            catch
            {
                return false;
            }
        }

        public bool ZoomOut(Point unchange)
        {
            try
            {
                for (int i = 0; i < Controller.MapLegends.Count; i++)
                {
                    if (Controller.MapLegends[i].ByScale == true)
                    {
                        switch (Controller.MapLegends[i].Legend_Type)
                        {
                            case CLegend.LegendType.CPointLegend:
                                ((CPointLegend)Controller.MapLegends[i]).size /= (float)Displayscale;
                                break;
                            case CLegend.LegendType.CLineLegend:
                                ((CLineLegend)Controller.MapLegends[i]).line_weight /= (float)Displayscale;
                                break;
                            case CLegend.LegendType.CPolygonLegend:
                                ((CPolygonLegend)Controller.MapLegends[i]).border_weight /= (float)Displayscale;
                                break;
                            case CLegend.LegendType.CTextLegend:
                                ((CTextLegend)Controller.MapLegends[i]).font = new Font("SimHei", ((CTextLegend)Controller.MapLegends[i]).font.Size / (float)Displayscale);
                                break;
                        }
                    }
                }

                PointF unchange_real = new PointF();
                unchange_real = Transformer.FromScreen(unchange);
                Displayscale=Displayscale/1.5;//默认缩小一倍
                if (Controller.MyMap.Unit == "英寸")
                {
                    OffsetX = unchange_real.X - unchange.X / (Displayscale * 96);
                    OffsetY = unchange.Y / (Displayscale * 96) + unchange_real.Y;
                }
                else
                {
                    OffsetX = unchange_real.X - unchange.X * 2.54 / (Displayscale * 96);
                    OffsetY = unchange.Y * 2.54 / (Displayscale * 96) + unchange_real.Y;
                }
                Transformer.Displayscale = Displayscale;
                Transformer.OffsetX = OffsetX;
                Transformer.OffsetY = OffsetY;

                   //如果图例是byscale的，则使图例跟着放大
                for (int i = 0; i < Controller.MapLegends.Count; i++)
                {
                    if (Controller.MapLegends[i].ByScale == true)
                    {
                        switch (Controller.MapLegends[i].Legend_Type)
                        {
                            case CLegend.LegendType.CPointLegend:
                                ((CPointLegend)Controller.MapLegends[i]).size *= (float)Displayscale;
                                CPointLegend legend = (CPointLegend)Controller.MapLegends[i];
                                Bitmap preview = new Bitmap((int)(legend.size) + 2, (int)(legend.size) + 2);//画笔默认为两个像素的宽度

                                Graphics gra = Graphics.FromImage(preview);

                                Pen pen = new Pen(legend.color, 2);
                                SolidBrush brush = new SolidBrush(legend.color);
                                Point[] points = new Point[4];
                                //三角形坐标
                                points[0].X = (int)(0.5 * legend.size); points[0].Y = (int)(legend.size - Math.Sqrt(3) * 0.5 * legend.size);
                                points[1].X = (int)(legend.size); points[1].Y = (int)(legend.size);
                                points[2].X = 0; points[2].Y = (int)(legend.size);
                                points[3] = points[0];
                                float tempx;
                                tempx = (float)(legend.size);
                                if (legend.Point_Style == CPointLegend.PointStyle.circle)
                                    gra.DrawEllipse(pen, 0, 0, tempx, tempx);
                                else if (legend.Point_Style == CPointLegend.PointStyle.filled_circle)
                                    gra.FillEllipse(brush, 0, 0, tempx, tempx);
                                else if (legend.Point_Style == CPointLegend.PointStyle.rectangle)
                                    gra.DrawRectangle(pen, 0, 0, legend.size, legend.size);
                                else if (legend.Point_Style == CPointLegend.PointStyle.filled_rectangle)
                                    gra.FillRectangle(brush, 0, 0, tempx, tempx);
                                else if (legend.Point_Style == CPointLegend.PointStyle.triangle)
                                    gra.DrawLines(pen, points);
                                else if (legend.Point_Style == CPointLegend.PointStyle.filled_triangle)
                                    gra.FillPolygon(brush, points);
                                else
                                    gra.DrawEllipse(pen, 0, 0, tempx, tempx);
                                ((CPointLegend)Controller.MapLegends[i]).cpointlegend = preview;
                                break;
                            case CLegend.LegendType.CLineLegend:
                                ((CLineLegend)Controller.MapLegends[i]).line_weight *= (float)Displayscale;
                                break;
                            case CLegend.LegendType.CPolygonLegend:
                                ((CPolygonLegend)Controller.MapLegends[i]).border_weight *= (float)Displayscale;
                                break;
                            case CLegend.LegendType.CTextLegend:
                                ((CTextLegend)Controller.MapLegends[i]).font = new Font("SimHei", ((CTextLegend)Controller.MapLegends[i]).font.Size * (float)Displayscale);
                                break;
                        }
                    }
                }
                return true;
            }
            catch
            {
                return false;
            }
        }

        //当前窗口的大小有问题！！
        public bool ZoomIn_Multi(Point start,Point end)
        {
            try
            {
                for (int i = 0; i < Controller.MapLegends.Count; i++)
                {
                    if (Controller.MapLegends[i].ByScale == true)
                    {
                        switch (Controller.MapLegends[i].Legend_Type)
                        {
                            case CLegend.LegendType.CPointLegend:
                                ((CPointLegend)Controller.MapLegends[i]).size /= (float)Displayscale;
                                break;
                            case CLegend.LegendType.CLineLegend:
                                ((CLineLegend)Controller.MapLegends[i]).line_weight /= (float)Displayscale;
                                break;
                            case CLegend.LegendType.CPolygonLegend:
                                ((CPolygonLegend)Controller.MapLegends[i]).border_weight /= (float)Displayscale;
                                break;
                            case CLegend.LegendType.CTextLegend:
                                ((CTextLegend)Controller.MapLegends[i]).font = new Font("SimHei", ((CTextLegend)Controller.MapLegends[i]).font.Size / (float)Displayscale);
                                break;
                        }
                    }
                }

                Point middle = new Point();
                middle.X = (start.X + end.X) / 2;
                middle.Y = (start.Y + end.Y) / 2;
                PointF middle_real = new PointF();
                middle_real = Transformer.FromScreen(middle);
                Point screen_middle = new Point();
                screen_middle.X = (int)Controller.View_Width / 2;
                screen_middle.Y = (int)Controller.View_Height / 2;
                if (Controller.MyMap.Unit == "英寸")
                {   //竖直方向较宽 
                    if (Convert.ToDouble(end.Y - start.Y) / Convert.ToDouble(end.X - start.X) >Convert.ToDouble( Controller.View_Height) /Convert.ToDouble( Controller.View_Width))
                    {
                        Displayscale = Displayscale * Convert.ToDouble(Controller.View_Height) / Convert.ToDouble(end.Y - start.Y);  //改变DisplayScale
                    }
                    else
                    {
                        Displayscale = Displayscale * Convert.ToDouble(Controller.View_Width) / Convert.ToDouble(end.X - start.X);  //改变DisplayScale
                    }
                    OffsetX = middle_real.X - screen_middle.X / (Displayscale * 96);
                    OffsetY = screen_middle.Y / (Displayscale * 96) + middle_real.Y;
                }
                else
                {
                    if (Convert.ToDouble(end.Y - start.Y) / Convert.ToDouble(end.X - start.X) > Convert.ToDouble(Controller.View_Height) / Convert.ToDouble(Controller.View_Width))
                    {
                        Displayscale = Displayscale * Convert.ToDouble(Controller.View_Height) / Convert.ToDouble(end.Y - start.Y);  //改变DisplayScale
                    }
                    else
                    {
                        Displayscale = Displayscale * Convert.ToDouble(Controller.View_Width) / Convert.ToDouble(end.X - start.X);  //改变DisplayScale
                    }
                    OffsetX = middle_real.X - screen_middle.X * 2.54 / (Displayscale * 96);
                    OffsetY = screen_middle.Y * 2.54 / (Displayscale * 96) + middle_real.Y;
                }
                Transformer.Displayscale = Displayscale;
                Transformer.OffsetX = OffsetX;
                Transformer.OffsetY = OffsetY;

                   //如果图例是byscale的，则使图例跟着放大
                for (int i = 0; i < Controller.MapLegends.Count; i++)
                {
                    if (Controller.MapLegends[i].ByScale == true)
                    {
                        switch (Controller.MapLegends[i].Legend_Type)
                        {
                            case CLegend.LegendType.CPointLegend:
                                ((CPointLegend)Controller.MapLegends[i]).size *= (float)Displayscale;
                                CPointLegend legend = (CPointLegend)Controller.MapLegends[i];
                                Bitmap preview = new Bitmap((int)(legend.size) + 2, (int)(legend.size) + 2);//画笔默认为两个像素的宽度

                                Graphics gra = Graphics.FromImage(preview);

                                Pen pen = new Pen(legend.color, 2);
                                SolidBrush brush = new SolidBrush(legend.color);
                                Point[] points = new Point[4];
                                //三角形坐标
                                points[0].X = (int)(0.5 * legend.size); points[0].Y = (int)(legend.size - Math.Sqrt(3) * 0.5 * legend.size);
                                points[1].X = (int)(legend.size); points[1].Y = (int)(legend.size);
                                points[2].X = 0; points[2].Y = (int)(legend.size);
                                points[3] = points[0];
                                float tempx;
                                tempx = (float)(legend.size);
                                if (legend.Point_Style == CPointLegend.PointStyle.circle)
                                    gra.DrawEllipse(pen, 0, 0, tempx, tempx);
                                else if (legend.Point_Style == CPointLegend.PointStyle.filled_circle)
                                    gra.FillEllipse(brush, 0, 0, tempx, tempx);
                                else if (legend.Point_Style == CPointLegend.PointStyle.rectangle)
                                    gra.DrawRectangle(pen, 0, 0, legend.size, legend.size);
                                else if (legend.Point_Style == CPointLegend.PointStyle.filled_rectangle)
                                    gra.FillRectangle(brush, 0, 0, tempx, tempx);
                                else if (legend.Point_Style == CPointLegend.PointStyle.triangle)
                                    gra.DrawLines(pen, points);
                                else if (legend.Point_Style == CPointLegend.PointStyle.filled_triangle)
                                    gra.FillPolygon(brush, points);
                                else
                                    gra.DrawEllipse(pen, 0, 0, tempx, tempx);
                                ((CPointLegend)Controller.MapLegends[i]).cpointlegend = preview;
                                break;
                            case CLegend.LegendType.CLineLegend:
                                ((CLineLegend)Controller.MapLegends[i]).line_weight *= (float)Displayscale;
                                break;
                            case CLegend.LegendType.CPolygonLegend:
                                ((CPolygonLegend)Controller.MapLegends[i]).border_weight *= (float)Displayscale;
                                break;
                            case CLegend.LegendType.CTextLegend:
                                ((CTextLegend)Controller.MapLegends[i]).font = new Font("SimHei", ((CTextLegend)Controller.MapLegends[i]).font.Size * (float)Displayscale);
                                break;
                        }
                    }
                }

                return true;
            }
            catch
            {
                return false;
            }
        }

        public bool ZoomToLayer()
        {
            try
            {
                for (int i = 0; i < Controller.MapLegends.Count; i++)
                {
                    if (Controller.MapLegends[i].ByScale == true)
                    {
                        switch (Controller.MapLegends[i].Legend_Type)
                        {
                            case CLegend.LegendType.CPointLegend:
                                ((CPointLegend)Controller.MapLegends[i]).size /= (float)Displayscale;
                                CPointLegend legend = (CPointLegend)Controller.MapLegends[i];
                                Bitmap preview = new Bitmap((int)(legend.size) + 2, (int)(legend.size) + 2);//画笔默认为两个像素的宽度

                                Graphics gra = Graphics.FromImage(preview);

                                Pen pen = new Pen(legend.color, 2);
                                SolidBrush brush = new SolidBrush(legend.color);
                                Point[] points = new Point[4];
                                //三角形坐标
                                points[0].X = (int)(0.5 * legend.size); points[0].Y = (int)(legend.size - Math.Sqrt(3) * 0.5 * legend.size);
                                points[1].X = (int)(legend.size); points[1].Y = (int)(legend.size);
                                points[2].X = 0; points[2].Y = (int)(legend.size);
                                points[3] = points[0];
                                float tempx;
                                tempx = (float)(legend.size);
                                if (legend.Point_Style == CPointLegend.PointStyle.circle)
                                    gra.DrawEllipse(pen, 0, 0, tempx, tempx);
                                else if (legend.Point_Style == CPointLegend.PointStyle.filled_circle)
                                    gra.FillEllipse(brush, 0, 0, tempx, tempx);
                                else if (legend.Point_Style == CPointLegend.PointStyle.rectangle)
                                    gra.DrawRectangle(pen, 0, 0, legend.size, legend.size);
                                else if (legend.Point_Style == CPointLegend.PointStyle.filled_rectangle)
                                    gra.FillRectangle(brush, 0, 0, tempx, tempx);
                                else if (legend.Point_Style == CPointLegend.PointStyle.triangle)
                                    gra.DrawLines(pen, points);
                                else if (legend.Point_Style == CPointLegend.PointStyle.filled_triangle)
                                    gra.FillPolygon(brush, points);
                                else
                                    gra.DrawEllipse(pen, 0, 0, tempx, tempx);
                                ((CPointLegend)Controller.MapLegends[i]).cpointlegend = preview;
                                break;
                            case CLegend.LegendType.CLineLegend:
                                ((CLineLegend)Controller.MapLegends[i]).line_weight /= (float)Displayscale;
                                break;
                            case CLegend.LegendType.CPolygonLegend:
                                ((CPolygonLegend)Controller.MapLegends[i]).border_weight /= (float)Displayscale;
                                break;
                            case CLegend.LegendType.CTextLegend:
                                ((CTextLegend)Controller.MapLegends[i]).font = new Font("SimHei", ((CTextLegend)Controller.MapLegends[i]).font.Size / (float)Displayscale);
                                break;
                        }
                    }
                }

                Displayscale = 1;
                OffsetX = 0;      //Offset为屏幕左上角的实际坐标
                OffsetY = Controller.MyMap.Height;
                Transformer.Displayscale = Displayscale;
                Transformer.OffsetX = OffsetX;
                Transformer.OffsetY = OffsetY;
                return true;
            }
            catch
            {
                return false;
            }
        }

        public bool Pan(Point start, Point end)
        {
            PointF start_real = Transformer.FromScreen(start);
            PointF end_real = Transformer.FromScreen(end);
            OffsetX = OffsetX + start_real.X - end_real.X;
            OffsetY = OffsetY + start_real.Y - end_real.Y;
            Transformer.OffsetX = OffsetX;
            Transformer.OffsetY = OffsetY;
            return true;
        }
    }

}
